package com.chen.demo.cas.controller;

import lombok.SneakyThrows;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class TestController {
    @SneakyThrows
    @GetMapping(value = "/login")
    public String login(HttpServletRequest  request, HttpServletResponse response) {
        //只是为了跳转到cas服务端的登录页面  登录成功后会跳回此页
        //可根据自身系统进行代码增强
        return "登录成功";
    }
    @GetMapping(value = "/logout")
    public void logout() {
        //只是为了跳转到cas服务端的登出页面
        //可根据自身系统进行代码增强
    }

    @PreAuthorize("hasRole('HELLO')")
    @GetMapping(value = "/hello")
    public String hello() {
        world();
        //登录成功后，访问此接口，如果正常返回hello word，表示已授权成功
        return "拥有hello权限";
    }

    @PreAuthorize("hasRole('WORLD')")
    @GetMapping(value = "/world")
    public String world() {
        return "拥有world权限";
    }

    @PreAuthorize("#age>=18")
    @GetMapping(value = "/none/{age}")
    public String age(@PathVariable Integer age) {
        return "年龄受限";
    }
}
